<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>审批管理</title>
    <link th:replace="/comm/header :: header"/>
    <script type="text/javascript">
        function formatOp1(val, row, index) {
            var str = '';
            if ($("#tong").length > 0) {
                str += $.formatString('<a href="javascript:void(0)" class="res-easyui-linkbutton-edit success"   onclick="tongFun(\'{0}\',{1});" >通过</a>', row.id,row.opid);
            }
            if ($("#exit").length > 0) {
                str += $.formatString('<a  href="javascript:void(0)" class="role-easyui-linkbutton-del error"  onclick="editFun(\'{0}\');" >驳回</a>', index);
            }
            return str;
        }

        function rowStyler(index, row) {
            if (row.sex == 1) {
                return 'background-color:#6293BB;color:#fff;'; // return inline style
            } else {
                return 'color:green;';
            }
        }

        function menuSetFun() {
            var rolesArr = roles.split(",");
            $("#dlg2").dialog("open").dialog("setTitle", "选择角色");
            $("#dg2").datagrid({
                url: '/fin/shen/list',
                method: 'get',
                onLoadSuccess: function (data) {
                    //console.dir(data)
                    var allRows = $("#dg2").datagrid("getRows");
                    for (var i = 0; i < allRows.length; i++) {
                        var name = allRows[i].cname;
                        if ($.inArray(name, rolesArr) >= 0) {
                            $("#dg2").datagrid("checkRow", i);
                        }
                    }
                }
            });
            $("#userId").val(userId);
        }

        function searchUser() {
            $("#dg").datagrid('load', {
                "name": $("#s_userName").val()
            });
        }


        function closeUserDialog() {
            $("#dlg").dialog("close");
            $('#fm').form('clear');
            $('#rb').radiobutton({
                checked: true
            });

        }

        function saveUser() {
            $("#fm").form("submit", {
                url: "/fin/shen/save",
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.flag) {
                        message("保存成功");
                        $('#fm').form('clear');
                        $("#dg").datagrid("reload");
                        $("#dlg").dialog("close");

                    } else {
                        message(result.message)
                    }
                }
            });
        }
        function formatterName(value,row) {
            return row.captain.cname;
        }
        function formatterPhone(value,row) {
            return row.captain.phone;
        }
        function formatterBank(value,row) {
            return row.captain.cdepositBank;
        }
        function formatterAccount(value,row) {
            return row.captain.account;
        }
        function formatterStatus(value,row,index) {
            if(row.cstatus==0){
                return '未申请';
            }else if(row.cstatus==1){
                return '申请中';
            }else if(row.cstatus==2){
                return '申请成功';
            }else{
                return '驳回';
            }
        }

        function openUserAddDialog() {
            $("#dlg").dialog("open").dialog("setTitle", "添加用户信息");

        }

        function editFun(index) {
            $("#dg").datagrid('selectRow', index);
            var row = $("#dg").datagrid('getSelected');
            $("#creason1").val("驳回");
            $("#dlg").dialog("open").dialog("setTitle", "驳回原因");
            $("#fm").form("load", row);
        }


        function tongFun(id,opid) {
            $.messager.confirm("系统提示", "您确定要通过申请吗?", function (r) {
                if (r) {
                    $.post("/fin/shen/tong", {id: id,opid:opid}, function (result) {
                        if (result.flag) {
                            message("审核通过！")
                            $("#dg").datagrid("reload");
                        } else {
                            message(result.message)
                        }
                    }, "json");
                }
            });
        }
        $(function(){   //第二步,编写js代码,在页面加载完成后开始自定义校验
            $.extend($.fn.validatebox.defaults.rules, {
                telephone: {    //第三步,选中校验谁
                    validator: function(value){    //第四步, 具体编写校验规则
                        var reg = /^1[3,5,7,8][0-9]{9}$/;

                        return reg.test(value);

                    },
                    message: '请输入正确的手机号!'   //第五步,如果输入内容不符合校验规则,出现的提示语.
                }
            });
        });

        function saveRoleSet() {
            var id = $("#id").val();

            var selectedRows = $("#dg2").datagrid("getSelections");
            var strRolenames = [];
            var roleIds = [];
            for (var i = 0; i < selectedRows.length; i++) {
                strRolenames.push(selectedRows[i].cname);
                roleIds.push(selectedRows[i].id);
            }
            var rolenames = strRolenames.join(",");
            $.post("/sys/user/savaUserRole", {cnames: rolenames, roles:roleIds.join(","),userId: userId}, function (result) {
                if (result.flag) {
                    closeRoleSetDialog();
                    $("#dg").datagrid("reload");
                    message("保存成功")
                } else {
                    message("提交失败，请联系管理员！");
                }
            }, "json");
        }

        function closeRoleSetDialog() {
            $("#dlg2").dialog("close");
        }
        function formatter3(value, row, index) {
            if (row.cstatus == 2) {
                return '已驳回'
            } else if (row.cstatus == 1) {
                return '申请中';
            } else if(row.cstatus==3){
                return '申请成功';
            }else if(row.cstatus==4){
                return "撤销";
            }else{
                return '未申请';
            }
        }


    </script>
</head>
<body style="margin: 1px">
<shiro:hasPermission name="approval:manage:reject">
    <input type="hidden" id="exit"/>
</shiro:hasPermission>
<shiro:hasPermission name="approval:manage:pay">
    <input type="hidden" id="tong"/>
</shiro:hasPermission>

<table id="dg" title="审批管理" class="easyui-datagrid"
       fitColumns="true" pagination="true" rownumbers="true" singleSelect="true" method="get"
       url="/fin/shen/listAll" fit="true" toolbar="#tb">
    <thead>
    <th field="id" width="20" align="center" hidden>编号</th>
    <th field="applicant" width="50" align="center">申请人</th>
    <th field="phone" width="50" align="center">电话</th>
    <th field="eid" width="50" align="center">企业id</th>
    <th field="payment" width="50" align="center">总计</th>
    <th field="ctotal" width="50" align="center">趟数</th>
    <th field="cstatus" width="50" align="center" formatter="formatter3">状态</th>
    <th field="opid" width="80" align="center" hidden>申请编号</th>
    <th field="lastime" width="150" align="center">申请时间</th>
    <th field="creason" width="150" align="center" hidden>审批原因</th>
    <th field="aa" width="200" align="center" formatter="formatOp1">操作</th>
    </thead>
</table>

<div id="tb">
    <!--<shiro:hasPermission name="sys:user:add">
        <div  style="display: inline-block;">

            <a href="javascript:openUserAddDialog()" class="easyui-linkbutton primary" iconCls="fa fa-plus" plain="true">添加</a>

        </div>-->
    </shiro:hasPermission>
    <shiro:hasPermission name="approval:manage:search">
        <div  style="display: inline-block;float:right">
            &nbsp;队长姓名：&nbsp;<input type="text" id="s_userName" size="20"  class="easyui-validatebox easyui-textbox"  data-options="validType:'length[3,10]',iconAlign:'left', iconCls:'fa fa-user'"
                                   onkeydown="if(event.keyCode==13) searchUser()"/>
            <a href="javascript:searchUser()" class="easyui-linkbutton primary" iconCls="fa fa-search" plain="true">搜索</a>
        </div>
    </shiro:hasPermission>
</div>


<div id="dlg" class="easyui-dialog" style="width: 450px;height: 400px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons" data-options="onClose:function(){resetValue()}">
    <form id="fm" method="post">
        <input type="hidden" name="id"/>
        <input type="hidden" name="opid">
        <input type="hidden" name="creason">
        <table cellspacing="8px">
            <tr>
                <td valign="top">驳回原因：</td>
                <td>
                    <textarea rows="4" cols="40" id="creason1" name="creason1"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:saveUser()" class="easyui-linkbutton" iconCls="fa fa-check">保存</a>
    <a href="javascript:closeUserDialog()" class="easyui-linkbutton" iconCls="fa fa-times">关闭</a>
</div>
<div id="dlg2" class="easyui-dialog" style="width: 500px;height: 350px;"
     closed="true" buttons="#dlg-buttons2">
    <input type="hidden" id="userId" name="userId"/>
    <table id="dg2" class="easyui-datagrid" fitColumns="true" method="post" fit="true">
        <thead>
        <th field="cb" checkbox="true" align="center"></th>
        <th field="id" width="20" align="left" hidden="true">编号</th>
        <th field="creason" width="200" align="left">驳回原因</th>
        </thead>
    </table>
</div>

<div id="dlg-buttons2">
    <a href="javascript:saveRoleSet()" class="easyui-linkbutton" iconCls="fa fa-check">保存</a>
    <a href="javascript:closeRoleSetDialog()" class="easyui-linkbutton" iconCls="fa fa-times">关闭</a>
</div>
<script type="text/javascript">
    $(function () {
        $("#dg").datagrid({
            onLoadSuccess: function (data) {
                $('.res-easyui-linkbutton-edit').linkbutton({text: '通过', plain: true, iconCls: 'fa fa-pencil'});
                $('.role-easyui-linkbutton-del').linkbutton({text: '驳回', plain: true, iconCls: 'fa fa-trash-o'});
                // $('#dg').datagrid('fixRowHeight');
            }
        });
    })
</script>
</body>
</html>